package LeetCode;

public class Code75_sortColors {
    // 1.计数排序
    public void sortColors(int[] nums) {
        if (nums == null || nums.length == 0){
            return;
        }
        int sum0 = 0;
        int sum1 = 0;
        int sum2 = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 0){
                sum0++;
            }
            if (nums[i] == 1){
                sum1++;
            }
            if (nums[i] == 2){
                sum2++;
            }
        }

        int i = 0;
        while (sum0 > 0){
            nums[i] = 0;
            sum0--;
            i++;
        }
        while (sum1 > 0){
            nums[i] = 1;
            sum1--;
            i++;
        }
        while (sum2 > 0){
            nums[i] = 2;
            sum2--;
            i++;
        }
    }
    public void sortColors2(int[] nums) {
        if (nums == null || nums.length == 0) {
            return;
        }
        int p0 = 0;
        int cur = 0;
        int p2 = nums.length - 1;

        while (cur <= p2){
            if (nums[cur] == 2){
                nums[cur] = nums[p2];
                p2--;
            } else if (nums[cur] == 0){
                nums[cur] = nums[p0];
                nums[p0] = 0;
                p0++;
                cur++;
            } else {
                cur++;
            }
        }
    }



    public static void main(String[] args) {
        Code75_sortColors code75_sortColors = new Code75_sortColors();
        int[] nums = new int[]{1,2,0};
        code75_sortColors.sortColors2(nums);
        for (int i = 0; i < nums.length; i++) {
            System.out.println(nums[i]);
        }
    }
}
